<template>
	<view>
		<view class="shadow u-padding-30 u-flex u-row-between">
			<view class="u-flex" v-if="address.id">
				<u-icon name="map-fill" color="#2D9AFE" size="40"></u-icon>
				<view style="width: 500rpx;margin-left: 20rpx;">
					<view class="u-flex">
						<view style="font-size: 32rpx;font-weight: bold;">{{address.realname}}</view>
						<view style="color: #AEAEAE;margin-left: 40rpx;">{{address.phone}}</view>
					</view>
					<view class="">
						地址：{{address.province}}{{address.city}}{{address.county}}{{address.address}}
					</view>
				</view>
			</view>
			<view class="change" style="color: #AEAEAE;" v-if="address.id">
				<navigator url="../address/index">切换</navigator>
			</view>
			<view v-else style="font-size: 32rpx;font-weight: bold;margin-left: 20rpx;" hover-class="none">
				<navigator url="../address/addAddress">请先选择收货地址</navigator>
			</view>
		</view>

		<view class="u-padding-left-30 u-padding-right-30">
			<view class="u-margin-top-40 u-margin-bottom-40">
				<u-section title="产品信息" :right="false" color="#2D9AFE" font-size="35"></u-section>
			</view>


			<!-- 商品 -->
			<view v-for="(item,index) in list" :key="index" class=" u-flex u-padding-20  list">
				<u-image :src="item.cover" width="100" height="100"></u-image>
				<view class="u-margin-left-20" style="width: 600rpx;">
					<view style="font-weight: bold;">{{item.goodsName}}</view>
					<view class="u-flex u-row-between u-margin-top-20" v-if="item.goodsType==1">
						<view style="color: #FABE00;">￥{{item.price}}</view>
					</view>
					
					<view class="u-flex u-row-between u-margin-top-20" v-if="item.goodsType==2">
						<view style="font-size: 26rpx;color: #007AFF;font-weight: bold;">{{item.integral}}积分<text
								v-if="Number(item.price)>0"> +{{item.price}}元</text> </view>
					</view>
					
					
				</view>
			</view>

			<u-cell-group>
				<u-cell-item title="订购数量" :arrow="false" >
					<view slot="right-icon">
						<u-number-box v-model="value" @change="valChange" bg-color="#2D9AFE" color="#fff" min="1">
						</u-number-box>
					</view>
				</u-cell-item>
				<u-cell-item title="金额总计" :arrow="false">
					<view slot="right-icon" style="color: #FF6D3F;font-size: 40rpx;">
						￥{{allMoneny}}
					</view>
				</u-cell-item>

				<u-cell-item title="订单备注" :arrow="false" title-width="600">
					<u-input slot="label" type="textarea" placeholder="请填写备注信息" v-model="marker" />
				</u-cell-item>
			</u-cell-group>
		</view>

		<view class="bottomFixs" @click="ok">立即订购</view>
	</view>
</template>

<script>
	import payway from '@/common/wxpay.js';
	export default {
		data() {
			return {
				address: {},
				list: [],
				value: 1,
				elseInfo: '',
				address_id: '',
				allMoneny:''
			}
		},
		onLoad(options) {
			if (options.info) {
				var info = JSON.parse(decodeURIComponent(options.info))
				this.list = info.goods;
				this.allMoneny=info.payMoney
				this.elseInfo = info;

			}
		},
		onShow() {
			this.getAddress()
		},
		methods: {
			valChange(e){
				
				this.allMoneny=(e.value* Number(this.list[0].price)).toFixed(2) ;
			},
			// 获取地址信息
			getAddress() {
				this.$u.post('/api/user/address', {
					address_id: this.address_id
				}, {}).then(res => {

					if (Array.isArray(res)) {
						if (res.length) {
							this.address = res[0];
							this.address_id = res[0].id;
						}
					} else {
						this.address = res;
					}

				}).catch(err => {})

			},
			ok() {
				if(!this.address_id){
					this.$u.toast("请先填写地址")
					return
				}
				this.$u.post('/api/indent/buy', {
					address_id: this.address_id,
					goods_id: this.list[0].id,
					num: this.value,
				}, {}).then(res => {

					var packages = res.package;
					var {
						noncestr,
						timestamp,
						signtype,
						paysign
					} = res;
					payway.wx(timestamp, noncestr, packages, signtype, paysign).then(
						res => {
							this.$refs.uToast.show({
								title: '购买成功',
								type: 'success',
								callback: () => {
									uni.redirectTo({
										url:""
									})
								}
							})
						}).catch(err => {})



				}).catch(err => {})

			}
		}
	}
</script>

<style>
	.bottomFixs {
		background: #3464D6;
		color: #fff;
		height: 100rpx;
		text-align: center;
		line-height: 100rpx;
		z-index: 999;
		width: 100%;
		position: fixed;
		bottom: 0;

	}
</style>
